<script setup>
import { ref, defineEmits, defineProps, onMounted } from "vue"
import selectperson from "@/views/material/workshop/selectperson.vue"
import request from '@/api/request'
import { ElMessage } from 'element-plus'
import { useRouter } from "vue-router"

let router = useRouter()
let emits = defineEmits(["ref-table"])
let props = defineProps(["data1"])
let data = ref()

let Process = ref([])
let workshop = ref({
  workshopcId: '',
  workshopName: '',
  area: 1,
  person: '',
  isEnabled: '0', // 默认启用
  remark: ''
})

// 自动生成功能相关
const autoGenerate = ref(false)

// 组件挂载时如果开启自动生成则生成编码
onMounted(() => {
  if (autoGenerate.value) {
    generateWorkshopId()
  }
})

// 生成车间编码 - 方案1: 使用随机数和时间组合
const generateWorkshopId = () => {
  const now = new Date()
  const year = now.getFullYear().toString().substr(2) // 取年份后两位
  
  const random = Math.floor(Math.random() * 1000).toString().padStart(3, '0')
  return `WS${year}${random}`
}


// 处理自动生成功能开关
const handleAutoGenerate = (val) => {
  if (val) {
    // 开启自动生成
    workshop.value.workshopcId = generateWorkshopId()
  } else {
    // 关闭自动生成，清空编码
    workshop.value.workshopcId = ''
  }
}

// 重新生成编码
const regenerateCode = () => {
  if (autoGenerate.value) {
    workshop.value.workshopcId = generateWorkshopId()
  }
}

let onSubmit = () => {
  // 表单验证
  if (!workshop.value.workshopcId) {
    ElMessage.warning('车间编码不能为空')
    return
  }
  
  if (!workshop.value.workshopName) {
    ElMessage.warning('车间名称不能为空')
    return
  }
  
  request({
    url: '/workshop/insert',
    method: 'post',
    data: {
      ...workshop.value,
    }
  }).then(res => {
    if (res.code !== 200) {
      ElMessage.error(res.message || res.data)
    } else {
      ElMessage.success('添加成功')
      emits("ref-table")
      // 如果开启自动生成，成功后生成新的编码
      if (autoGenerate.value) {
        workshop.value.workshopcId = generateWorkshopId()
        // 其他字段重置
        workshop.value.workshopName = ''
        workshop.value.area = 1
        workshop.value.person = ''
        workshop.value.remark = ''
      }
    }
  }).catch(error => {
    ElMessage.error('请求失败: ' + error.message)
  })
}

let num1 = ref(1)

const handleChange1 = (value) => {
  workshop.value.area = value
}

const color1 = ref('#409EFF')

request.post("/system/user/list", { size: 100000 }).then(res => {
})

let title = ref("")
let isVisible = ref(false)
let refTanle1 = (a) => {
  isVisible.value = false
  workshop.value.person = a.value.nickname
}

let select1 = () => {
  title.value = "人员选择"
  isVisible.value = true
}
</script>

<template>
  <div>
    <el-card>
      <template #header>
        <el-form :model="workshop" style="padding-left: 70px;" class="card_wrap">
          <el-form-item label="车间编码" class="c_item_big">
            <div style="display: flex; align-items: center;">
              <el-input 
                v-model="workshop.workshopcId" 
                style="width: 350px;"
                :readonly="autoGenerate"
                placeholder="请输入车间编码"
              ></el-input>
              <el-switch 
                v-model="autoGenerate"
                active-text="自动生成"
                @change="handleAutoGenerate"
                style="margin-left: 15px;"
              />
              <el-button 
                v-if="autoGenerate"
                @click="regenerateCode"
                style="margin-left: 10px;"
                size="small"
              >
                重新生成
              </el-button>
            </div>
          </el-form-item>
          
          <el-form-item label="车间名称" class="c_item_big">
            <el-input 
              v-model="workshop.workshopName" 
              style="width: 350px;" 
              placeholder="请输入车间名称"
            ></el-input>
          </el-form-item>
          
          <el-form-item label="面积" class="c_item_big">
            <el-input-number 
              v-model="workshop.area" 
              :min="1" 
              :max="2000" 
              @change="handleChange1" 
            />
          </el-form-item>
          
          <el-form-item label="负责人" class="c_item_big">
            <div style="display: flex; align-items: center;">
              <el-input 
                v-model="workshop.person" 
                style="width: 350px;"
                readonly
              ></el-input> 
              <el-button 
                type="primary" 
                size="default"  
                @click="select1()"
                style="margin-left: 10px;"
              >
                选择
              </el-button>
            </div>
          </el-form-item>
          
          <el-form-item label="是否启用" class="c_item_big">
            <el-radio-group v-model="workshop.isEnabled">
              <el-radio label="是" value="0">是</el-radio>
              <el-radio label="否" value="1">否</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="备注" class="c_item_big">
            <el-input 
              v-model="workshop.remark" 
              style="width: 350px;" 
              placeholder="请输入备注"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="onSubmit" class="right-bottom">添加</el-button>
          </el-form-item>
        </el-form>
      </template>
    </el-card>
    
    <div style="width:800px">
      <el-dialog 
        :title="title" 
        v-model="isVisible" 
        :destroy-on-close="true" 
        width="60%"
      >
        <selectperson @ref-table1="refTanle1" :data="data"></selectperson>
      </el-dialog>
    </div>
    
    <router-view/>
  </div>
</template>

<style scoped>
.c_item {
  flex: 1;
  margin: 0px 5px 15px 0;
  width: calc((100% - 10px) / 2);
  min-width: calc((100% - 10px) / 2);
  max-width: calc((100% - 10px) / 2);
}

.c_item_big {
  flex: 2;
  margin: 0px 5px 15px 0;
  width: calc((100% - 10px) / 1);
  min-width: calc((100% - 10px) / 1);
  max-width: calc((100% - 10px) / 1);
}

.card_wrap {
  width: 98%;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
}

.color {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
}

.right-bottom {
  margin-top: 10px;
  margin-left: 700px;
}

.right-bottom2 {
  margin-top: 20px;
  margin-left: 850px;
}
</style>